﻿using Peanut.Common.AttributeEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TlToolCordCreate
{
    /// <summary>
    /// ucOPCUaCodeCreate1.xaml 的交互逻辑
    /// </summary>
    public partial class ucOPCUaCodeCreate1 : UserControl
    {
        public ucOPCUaCodeCreate1()
        {
            InitializeComponent();
        }

        private void Button_CodeCreate(object sender, RoutedEventArgs e)
        {
           
            string code = "";
            if (classname.Text!="")
            {
                code += "public class "+classname.Text+"Entity\r\n";
                code += "{\r\n";
            }
            var soucodes = soucode.Text.Split("\r\n");
            foreach (var item in soucodes)
            {
                if (item=="")
                {
                    continue;
                }
                var rowcode = item.Split(" ");
                code += "    /// <summary>\r\n";
                code += "    /// 描述:" + rowcode[0] + "\r\n" ;
                code += "    /// 地址:" + rowcode[1] + "\r\n" ;
                code += "    /// </summary>\r\n";
                code += "    [ComOpcUaData(\"" + rowcode[1] +"\", \"" + rowcode[0] +"\", OprTypeEnum."+ fun1(rowcode[3]) + ", DefaultValue = false)]\r\n";
                code += "    public "+ fun2(rowcode[3]) + " "+ rowcode[4] + " { get; set; }\r\n";
            }
            if (classname.Text != "")
            {
                code += "}\r\n";
            }
            dstcode.Text = code;
        }
        private string fun1(string value)
        {
            if (value.ToLower().Contains("w"))
            {
                return "Write";
            }
            else
            {
                return "Read";
            }
        }
        private string fun2(string value)
        {
            if (value.ToLower() == "bool")
            {
                return "bool";
            }
            if (value.ToLower() == "int16")
            {
                return "Int16";
            }
            if (value.ToLower() == "byte")
            {
                return "byte";
            }
            return value;
        }

        private void Button_CopyCode(object sender, RoutedEventArgs e)
        {

        }
    }
}

